home *** CD-ROM | disk | FTP | other *** search
/ Nebula 1 / Nebula One.iso / Graphics / Viewers / aa_m68k_Intel_Only / ToyViewer1.2 / Source / getpixel.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-11-12  |  7.3 KB  |  345 lines

  1. #include <stdlib.h>
  2. #include "common.h"
  3. #include "save.h"
  4.  
  5. static commonInfo *comInfo;
  6. static unsigned char *rr, *gg, *bb, *aa;
  7.  
  8. static int cs0, cs1, elems, alpx, palp;
  9. static BOOL ismono;
  10. static int bufp, yline;
  11. static unsigned char *buffer[4];
  12. static int buffer_size = 0;
  13.  
  14. static int _cc;
  15. static short _pp[] = { 0, 0x55, 0xaa, 0xff };
  16. #define pigment(c)    (((_cc = ((c) & 0xf0)) == 0xf0) ? 0xff : _cc)
  17. #define pigment2(c)    (_pp[((c) & 0xc0) >> 6])
  18. /*
  19.     static int pigment(int cc)
  20.     {
  21.     int n = cc & 0xf0;
  22.     if (n == 0xf0) n = 0xff;
  23.     return n;
  24.     }
  25.  
  26.     static int pigment2(int cc)
  27.     {
  28.     static unsigned char tone[] = { 0, 0x55, 0xaa, 0xff };
  29.     return tone[cc >> 6];
  30.     }
  31. */
  32.  
  33.  
  34. int initGetPixel(commonInfo *cinf)
  35. {
  36.     comInfo = cinf;
  37.     if (comInfo->cspace == NX_OneIsBlackColorSpace)
  38.         cs0 = 0xff, cs1 = 0;
  39.     else
  40.         cs0 = 0, cs1 = 0xff;
  41.     ismono = (comInfo->numcolors == 1);
  42.     palp = 0;
  43.     if (comInfo->alpha) {
  44.         elems = 4;
  45.         alpx = ismono ? 1 : 3;
  46.     }else {
  47.         elems = 3;
  48.         alpx = 0;
  49.     }
  50.     if (comInfo->width > buffer_size) {
  51.         int i;
  52.         unsigned char *p;
  53.         if (comInfo->width > MAXWidth)
  54.             return Err_SAV_IMPL;
  55.         if (buffer_size > 0) free((void *)buffer[0]);
  56.         buffer_size = (comInfo->width + 7) & 0xfff8;
  57.         p = (unsigned char *)malloc(buffer_size * 4);
  58.         if (p == NULL) {
  59.             buffer_size = 0;
  60.             return Err_MEMORY;
  61.         }
  62.         for (i = 0; i < 4; i++) {
  63.             buffer[i] = p;
  64.             p += buffer_size;
  65.         }
  66.     }
  67.     return 0;
  68. }
  69.  
  70. void resetPixel(unsigned char **planes, int y)
  71. {
  72.     rr = planes[0];
  73.     if (ismono || !comInfo->isplanar) {
  74.         gg = bb = rr;
  75.     }else {
  76.         gg = planes[1];
  77.         bb = planes[2];
  78.     }
  79.     aa = planes[alpx];
  80.     if (y > 0) {
  81.         int w = comInfo->xbytes * y;
  82.         rr += w;
  83.         gg += w;
  84.         bb += w;
  85.         aa += w;
  86.     }
  87.     yline = y;
  88.     bufp = MAXWidth;
  89. }
  90.  
  91. static int do_alpha(int c, int a)
  92. {
  93.     int n;
  94.     if (a >= 255) return c;
  95.     n = 255 - a + ((c * a) >> 8);    /* (256-c)*((256-a)/256)+c */
  96.     return (n >= 255) ? 255 : n;
  97. }
  98.  
  99. int getPalPixel(int *r, int *g, int *b)
  100. {
  101.     unsigned char *p;
  102.  
  103.     if (palp >= comInfo->palsteps)
  104.         return -1;
  105.     p = comInfo->palette[palp++];
  106.     switch (comInfo->bits) {
  107.     case 1:
  108.         *r = p[RED] ? 0xff : 0;
  109.         *g = p[GREEN] ? 0xff : 0;
  110.         *b = p[BLUE] ? 0xff : 0;
  111.         break;
  112.     case 2:
  113.         *r = pigment2(p[RED]);
  114.         *g = pigment2(p[GREEN]);
  115.         *b = pigment2(p[BLUE]);
  116.         break;
  117.     case 4:
  118.         *r = pigment(p[RED]);
  119.         *g = pigment(p[GREEN]);
  120.         *b = pigment(p[BLUE]);
  121.         break;
  122.     case 8:
  123.     default:
  124.         *r = p[RED];
  125.         *g = p[GREEN];
  126.         *b = p[BLUE];
  127.         break;
  128.     }
  129.     return 0;
  130. }
  131.  
  132. static int getNextLine(void)
  133. {
  134.     int i, x, mask, xbytes;
  135.  
  136.     if (++yline > comInfo->height)
  137.         return -1;    /* End of Image */
  138.     bufp = 0;
  139.     xbytes = comInfo->xbytes;
  140.  
  141.     if (comInfo->isplanar) {
  142.         if (comInfo->bits == 1) {
  143.         for (x = 0; x < xbytes; x++) {
  144.             for (mask = 0x80; mask; mask >>= 1) {
  145.             buffer[RED][bufp]   = (*rr & mask)? cs1 : cs0;
  146.             buffer[GREEN][bufp] = (*gg & mask)? cs1 : cs0;
  147.             buffer[BLUE][bufp]  = (*bb & mask)? cs1 : cs0;
  148.             bufp++;
  149.             }
  150.             rr++, gg++, bb++;
  151.         }
  152.         if (alpx) {
  153.             bufp = 0;
  154.             for (x = 0; x < xbytes; x++) {
  155.             for (mask = 0x80; mask; mask >>= 1)
  156.                 buffer[ALPHA][bufp++]  = (*aa & mask)? cs1 : cs0;
  157.             }
  158.             aa++;
  159.         }
  160.         }else if (comInfo->bits == 2) {
  161.         for (x = 0; x < xbytes; x++) {
  162.             for (i = 0; i < 8; i += 2) {
  163.             buffer[RED][bufp]   = pigment2(*rr << i);
  164.             buffer[GREEN][bufp] = pigment2(*gg << i);
  165.             buffer[BLUE][bufp]  = pigment2(*bb << i);
  166.             bufp++;
  167.             }
  168.             rr++, gg++, bb++;
  169.         }
  170.         if (alpx) {
  171.             bufp = 0;
  172.             for (x = 0; x < xbytes; x++) {
  173.             for (i = 0; i < 8; i += 2)
  174.                 buffer[ALPHA][bufp++] = pigment2(*aa << i);
  175.             aa++;
  176.             }
  177.         }
  178.         }else if (comInfo->bits == 4) {
  179.         for (x = 0; x < xbytes; x++) {
  180.             buffer[RED][bufp]   = pigment(*rr);
  181.             buffer[GREEN][bufp] = pigment(*gg);
  182.             buffer[BLUE][bufp]  = pigment(*bb);
  183.             bufp++;
  184.             buffer[RED][bufp]   = pigment(*rr++ << 4);
  185.             buffer[GREEN][bufp] = pigment(*gg++ << 4);
  186.             buffer[BLUE][bufp]  = pigment(*bb++ << 4);
  187.             bufp++;
  188.         }
  189.         if (alpx) {
  190.             bufp = 0;
  191.             for (x = 0; x < xbytes; x++) {
  192.             buffer[ALPHA][bufp++]  = pigment(*aa);
  193.             buffer[ALPHA][bufp++]  = pigment(*aa++ << 4);
  194.             }
  195.         }
  196.         }else /* 8 */ {
  197.         for (x = 0; x < xbytes; x++) {
  198.             buffer[RED][x]   = *rr++;
  199.             buffer[GREEN][x] = *gg++;
  200.             buffer[BLUE][x]  = *bb++;
  201.         }
  202.         if (alpx) {
  203.             for (x = 0; x < xbytes; x++)
  204.             buffer[ALPHA][x]  = *aa++;
  205.         }
  206.         }
  207.     }else if (ismono) { /* meshed mono */
  208.         if (comInfo->bits == 1) {
  209.         for (x = 0; x < xbytes; x++) {
  210.             for (mask = 0x80; mask; mask >>= 1) {
  211.             buffer[0][bufp] = buffer[1][bufp] = buffer[2][bufp]
  212.                 = (*rr & mask)? cs1 : cs0;
  213.             if (alpx) {
  214.                 mask >>= 1;
  215.                 buffer[ALPHA][bufp] = (*rr & mask)? cs1 : cs0;
  216.             }
  217.             bufp++;
  218.             }
  219.             rr++;
  220.         }
  221.         }else if (comInfo->bits == 2) {
  222.         for (x = 0; x < xbytes; x++) {
  223.             for (i = 0; i < 8; i += 2) {
  224.             buffer[0][bufp] = buffer[1][bufp] = buffer[2][bufp]
  225.                 = pigment2(*rr << i);
  226.             if (alpx) {
  227.                 i += 2;
  228.                 buffer[ALPHA][bufp] = pigment2(*rr << i);
  229.             }
  230.             bufp++;
  231.             }
  232.             rr++;
  233.         }
  234.         }else if (comInfo->bits == 4) {
  235.         if (alpx) {
  236.             for (bufp = 0; bufp < xbytes; bufp++) {
  237.             buffer[0][bufp] = buffer[1][bufp] = buffer[2][bufp]
  238.                     = pigment(*rr);
  239.             buffer[ALPHA][bufp] = pigment(*rr++ << 4);
  240.             }
  241.         }else {
  242.             int sft = 0;
  243.             x = 0;
  244.             for (bufp = 0; ; bufp++) {
  245.             buffer[0][bufp] = buffer[1][bufp] = buffer[2][bufp]
  246.                     = pigment(sft ? (*rr << 4) : *rr);
  247.             if (sft) {
  248.                 sft = 0, rr++;
  249.                 if (++x >= xbytes) break;
  250.             }else
  251.                 sft = 1;
  252.             }
  253.         }
  254.         }else /* 8 */ {
  255.         for (bufp = 0; bufp < xbytes; bufp++) {
  256.             buffer[0][bufp] = buffer[1][bufp]
  257.                     = buffer[2][bufp] = *rr++;
  258.             if (alpx) buffer[ALPHA][bufp] = *rr++;
  259.         }
  260.         }
  261.     }else { /* meshed color */
  262.         if (comInfo->bits == 1) {
  263.         i = x = 0;
  264.         mask = 0x80;
  265.         for ( ; ; ) {
  266.             buffer[i][bufp] = (*rr & mask)? cs1 : cs0;
  267.             if (++i >= elems)
  268.             i = 0, bufp++;
  269.             if ((mask >>= 1) == 0) {
  270.             mask = 0x80, rr++;
  271.             if (++x >= xbytes) break;
  272.             }
  273.         }
  274.         }else if (comInfo->bits == 2) {
  275.         i = x = 0;
  276.         mask = 0;
  277.         for ( ; ; ) {
  278.             buffer[i][bufp] = pigment2(*rr << mask);
  279.             if (++i >= elems)
  280.             i = 0, bufp++;
  281.             if ((mask += 2) == 8) {
  282.             mask = 0, rr++;
  283.             if (++x >= xbytes) break;
  284.             }
  285.         }
  286.         }else if (comInfo->bits == 4) {
  287.         int sft = 0;
  288.         i = x = 0;
  289.         for ( ; ; ) {
  290.             buffer[i][bufp] = pigment(sft ? (*rr << 4) : *rr);
  291.             if (++i >= elems)
  292.             i = 0, bufp++;
  293.             if (sft) {
  294.                 sft = 0, rr++;
  295.             if (++x >= xbytes) break;
  296.             }else
  297.                 sft = 1;
  298.         }
  299.         }else /* 8 */ {
  300.         for (x = 0; x < xbytes; x += elems) {
  301.             for (i = 0; i < elems; i++)
  302.             buffer[i][bufp] = *rr++;
  303.             bufp++;
  304.         }
  305.         }
  306.     }
  307.     bufp = 0;
  308.     return 0;
  309. }
  310.  
  311. int getPixel(int *r, int *g, int *b)
  312. {
  313.     if (bufp >= comInfo->width) {
  314.         if (getNextLine() != 0)
  315.             return -1;
  316.     }
  317.     if (alpx) {
  318.         *r = do_alpha(buffer[RED][bufp], buffer[ALPHA][bufp]);
  319.         *g = do_alpha(buffer[GREEN][bufp], buffer[ALPHA][bufp]);
  320.         *b = do_alpha(buffer[BLUE][bufp], buffer[ALPHA][bufp]);
  321.     }else {
  322.         *r = buffer[RED][bufp];
  323.         *g = buffer[GREEN][bufp];
  324.         *b = buffer[BLUE][bufp];
  325.     }
  326.     if (++bufp >= comInfo->width)
  327.         return 1;
  328.     return 0;
  329. }
  330.  
  331. int getPixelA(int *elm)
  332. {
  333.     if (bufp >= comInfo->width) {
  334.         if (getNextLine() != 0)
  335.             return -1;
  336.     }
  337.     elm[RED]   = buffer[RED][bufp];
  338.     elm[GREEN] = buffer[GREEN][bufp];
  339.     elm[BLUE]  = buffer[BLUE][bufp];
  340.     elm[ALPHA] = alpx ? buffer[ALPHA][bufp] : 0;
  341.     if (++bufp >= comInfo->width)
  342.         return 1;
  343.     return 0;
  344. }
  345.